package com.example.june.zhanghaoguanli;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longfly_example.e_scale.R;

import java.util.List;

public class Myadapter extends BaseAdapter {
    private List<ItemBean> mList;//数据源
    private LayoutInflater mInflater;//布局装载器对象

    // 通过构造方法将数据源与数据适配器关联起来
    // context:要使用当前的Adapter的界面对象
    public Myadapter(shopping context, List<ItemBean> list) {
        mList = list;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    //ListView需要显示的数据数量
    public int getCount() {
        return mList.size();
    }

    @Override
    //指定的索引对应的数据项
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    //指定的索引对应的数据项ID
    public long getItemId(int position) {
        return position;
    }

    @Override
    //返回每一项的显示内容
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局文件转化为View对象
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, parent, false); //加载布局
            holder = new ViewHolder();
            holder.itemTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.itemContent = (TextView) convertView.findViewById(R.id.tv_content);
            holder.itemImageResId = (ImageView) convertView.findViewById(R.id.iv_image);
            convertView.setTag(holder);
        }else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }
        ItemBean bean = mList.get(position);
        holder.itemTitle.setText(bean.getItemTitle());
        holder.itemContent.setText(bean.getItemContent());
        holder.itemImageResId.setImageResource(bean.getItemImageResId());

        return convertView;
    }
    private class ViewHolder {
        TextView itemTitle;
        TextView itemContent;
        ImageView itemImageResId;
    }
}
